package org.collection.newcollection;

import java.util.Map;
import java.util.Set;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

public class TableTest {
	
	public static void main(String[] args) {
		Table<String, Integer, Double> testTable = HashBasedTable.create();
		testTable.put("小米", 10, 0.12);
		testTable.put("小米", 20, 0.52);
		testTable.put("小米", 30, 0.82);
		testTable.put("大米", 10, 0.13);
		testTable.put("大米", 20, 0.32);
		testTable.put("大米", 30, 0.93);
		testTable.put("红米", 10, 0.53);
		testTable.put("红米", 20, 0.12);
		testTable.put("红米", 30, 0.63);
		
		Map<String, Map<Integer, Double>> rowMap = testTable.rowMap();
		System.out.println(rowMap.get("大米").get(10));
		System.out.println(rowMap.get("大米").get(20));
		
		Map<Integer, Double> oneRow = testTable.row("红米");
		for (Map.Entry<Integer, Double> entry : oneRow.entrySet()) {
			System.out.println(entry.getKey() + "|" + entry.getValue());
		}
		// （基于列的访问会比基于的行访问稍微低效点）
		Map<String, Double> oneColumn = testTable.column(10);
		for (Map.Entry<String, Double> entry : oneColumn.entrySet()) {
			System.out.println(entry.getKey() + "|" + entry.getValue());
		}
		
		Set<Table.Cell<String,Integer,Double>> cellSet = testTable.cellSet();
		for (Table.Cell<String,Integer,Double> cell : cellSet) {
			System.out.println(cell.getRowKey() + "|" + cell.getColumnKey() + "|" + cell.getValue());
		}
	}
	
	
}
